package ace.tool.generate.code.impl.generator.util;

/**
 * @author caspar
 * @date 2024/1/5 9:21
 */
public class GeneratorUtils {

  public static String getBasePackagePath(String entityClassPackagePath, int packageLevel) {
    String[] paths = entityClassPackagePath.split("\\.");
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < packageLevel; i++) {
      if (sb.length() > 0) {
        sb.append(".");
      }
      sb.append(paths[i]);
    }
    String basePackageName = sb.toString();
    return basePackageName;
  }

  public static String getBasePackagePath(Class entityClass) {
    Package entityPackage = entityClass.getPackage();
    return getBasePackagePath(entityPackage.getName(), 3);
  }

  public static String getParentPackageName(String packageName) {
    String[] packageArr = packageName.split("\\.");
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < packageArr.length - 1; i++) {
      if (sb.length() > 0) {
        sb.append(".");
      }
      sb.append(packageArr[i]);
    }
    return sb.toString();
  }
}
